<?
class htmlRenderer extends renderer{
	public $gravy;
	
	public function __construct($gravy){
		// call the construct
		parent::__construct($gravy);
	}
	
	public function render(){
		// start the render process
		$this->setHeadersForOutput();
		
		$VALIDATOR=$this->gravy->validator;
		$ERRORS=$this->gravy->errors;
		$DATA=$this->gravy->getTemplateData();
		$DOCUMENT=$this->getDocumentVariables();
		$GRAVY=$this->gravy;
		
		require_once(GRAVY_ROOT_DIR."functions/helpers.php");
		
		if($this->gravy->isFatalError){
			$this->renderError(404);
		}else{
			$IS_FATAL_ERROR=false;

			require_once($DOCUMENT["theme_file"]);
		}
	}
	
	public function renderError($statusCode){
		$VALIDATOR=$this->gravy->validator;
		$ERRORS=$this->gravy->errors;
		$DATA=$this->gravy->getTemplateData();
		$DOCUMENT=$this->getDocumentVariables();
		$GRAVY=$this->gravy;

		#$this->setHeadersForOutput($statusCode);
		$errorThemeFile=THEMES_ROOT_DIR.$this->gravy->error_theme.".".TEMPLATE_EXT;
		if(file_exists($errorThemeFile)){
			$IS_FATAL_ERROR=$this->gravy->isFatalError;
			require_once($errorThemeFile);
		}else{
			$this->gravy->debug("errorTheme",$errorThemeFile);
			die("ERROR $statusCode: No Templates Found!");
			exit();
		}
	}
	
	public function getDocumentVariables(){
		$result=array();
		
		$result["theme"]=$this->gravy->theme;
		$result["theme_file"]=THEMES_ROOT_DIR.$this->gravy->theme.".".TEMPLATE_EXT;
		
		$result["controller"]=$this->gravy->controller;
		$result["controller_file"]=VIEWS_ROOT_DIR.$this->gravy->view."/".$this->gravy->controller.".".TEMPLATE_EXT;
		
		$result["title"]=$this->gravy->pageTitle;
		
		$cache=$this->gravy->config->CACHE;
		if(is_string($cache)){
			$result["cache"]=$cache;
		}elseif($cache){
			$result["cache"]="";
		}else{
			$result["cache"]=($this->gravy->config->CACHE)?"r=":"r=".rand(0,10000);
		}

		return($result);
	}
};
?>